package com.b.a.b.crossword.domain.impl.util;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;

import com.b.a.b.crossword.domain.Case;
import com.b.a.b.crossword.domain.CaseDto;

public class Utilitaire {
public static void sauver(final Case[][] etat,String fichier) throws FileNotFoundException, IOException{
Integer nbrLigne=etat.length;
Integer nbrColonne=etat[0].length;
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(fichier));
out.writeObject(nbrLigne);
out.writeObject(nbrColonne);
for(int i=0;i<nbrLigne;i++)
	 for(int j=0;j<nbrColonne;j++)
		 out.writeObject(etat[i][j].getSaveData());
out.close();
}
@SuppressWarnings("unchecked")
public static Case[][] restaurer( String fichier) throws FileNotFoundException, IOException, ClassNotFoundException{
	ObjectInputStream in=new ObjectInputStream(new FileInputStream(fichier));
	Integer nbrLigne=(Integer) in.readObject();
	Integer nbrColonne=(Integer) in.readObject();
	Case[][] etat=new Case[nbrLigne][nbrColonne];
	for(int i=0;i<nbrLigne;i++)
		 for(int j=0;j<nbrColonne;j++)
			 (etat[i][j])= Utilitaire.caseDtoToCase((CaseDto) in.readObject());
	in.close();
	return etat;
}
private static Case from(HashMap<String, HashMap<String, Object>> readObject) {
	// TODO Auto-generated method stub
	return null;
}
public static int colorToInt(Color couleur) {
	// TODO Auto-generated method stub
return 	couleur.getRGB();
	/*int val=(couleur.getRed()*256+couleur.getGreen())*256+couleur.getBlue();
	return val;*/
}
public static Color IntTocolor(int couleur) {
	// TODO Auto-generated method stub
	/*int red=(couleur>>16)&255
			,green=(couleur>>8)&255,
			blue=couleur&255;*/
	 return new Color(couleur);
}
public static Case caseDtoToCase(CaseDto cdto){
	 Case res=new Case();
	 Field[] caseFields=res.getClass().getDeclaredFields();
	 Object argValue;
	 for (Field field : caseFields) {
		 
		 if(field.getModifiers()==Modifier.PRIVATE){
			 
		 String fieldName=field.getName();
		 if(fieldName=="listener")continue;
		 String setMethoName="set"+(fieldName.charAt(0)+"").toUpperCase()+fieldName.substring(1);
		 Field fieldCaseDto;
		try {
			fieldCaseDto = cdto.getClass().getDeclaredField(fieldName);
			fieldCaseDto.setAccessible(true);
			field.setAccessible(true);
			 Method m=res.getClass().getMethod(setMethoName, field.getType());
			 
			 if(field.getType() ==Color.class){
				 argValue=Utilitaire.IntTocolor(fieldCaseDto.getInt(cdto) );
				 
			 }else{
				 argValue= (fieldCaseDto.get(cdto) );
			 }
			 m.invoke(res, argValue);
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	}
}
	 System.err.println("res="+res);
	 res.mettreAJour();
	 res.ajouterecouteur();
	return res;
}
}
